home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1999 …ember: Reference Library / Apple Developer Reference Library (December 1999) (Disk 1).iso / pc / technical documentation / develop / additional articles / developing symbiotic apps / symbiotic samples / symbiotic client source / javelin.cw_mw9 / javelin.c < prev    next >
Encoding:
C/C++ Source or Header  |  1998-05-01  |  6.0 KB  |  230 lines

  1. #include <PPCToolbox.h>
  2. #include <EPPC.h>
  3. #include <Errors.h>
  4. #include <TextUtils.h>
  5. #include <Dialogs.h>
  6. #include <Strings.h>
  7.  
  8. #ifndef __GESTALTEQU__
  9.     #include <GestaltEqu.h>
  10. #endif
  11.  
  12. #ifndef __APPLEEVENTS__
  13.     #include <AppleEvents.h>
  14. #endif
  15.  
  16. #include "Symbiont.h"
  17. #include "Javelin.h"
  18. #include "JavelinEvents.h"
  19. #include "version.h"
  20.  
  21. // External Function Prototypes
  22. Boolean IsAppWindow(WindowPtr window);
  23. void New_Window(void);
  24. void snd_null(void);
  25. extern  pascal Boolean snd_version(void);
  26. extern  pascal OSErr HandleHeartBeat(AppleEvent *msg,AppleEvent *reply,long refcon);
  27. extern  pascal OSErr HandleQuit(AppleEvent *msg,AppleEvent *reply,long refcon);
  28. void AlertUser( short error );
  29.  
  30.  
  31.  
  32. // Local symbiont function prototypes
  33. void new_connection(void);
  34. void Javelin_AlertUser( short error );
  35. static pascal Boolean MyPPCBrowserFilter(LocationNamePtr, PortInfoPtr thePortInfo);
  36. void InstallAppleEventHandlers(void);
  37. static pascal OSErr HandleMsg(AppleEvent *msg,AppleEvent *reply,long refcon);
  38. void snd_interval(int interval);
  39.  
  40.  
  41. /* Global Variables */
  42. Str255 hostname;
  43. PPCSessRefNum TheSessionID;
  44. short ThePortRefNum;
  45. long sessionID=0;
  46.  
  47. // External globals
  48. extern TargetID TheTarget;
  49.  
  50. /* A DocumentRecord contains the WindowRecord for one of our document windows,
  51.    as well as the TEHandle for the text we are editing. Other document fields
  52.    can be added to this record as needed. For a similar example, see how the
  53.    Window Manager and Dialog Manager add fields after the GrafPort. */
  54. typedef struct {
  55.     WindowRecord    docWindow;
  56.     Str255            docTEXT[255];
  57.     ControlHandle    docVScroll;
  58.     ControlHandle    docHScroll;
  59.     ProcPtr            docClik;
  60. } DocumentRecord, *DocumentPeek;
  61.  
  62.  
  63. #pragma segment main
  64.  
  65. void new_connection(void)
  66. {
  67. PortInfoRec thePortInfo;
  68. OSErr err;
  69. int TheInterval=10;
  70.  
  71.     err = PPCInit();
  72.     err = PPCBrowser("\pSelect a Javelin Server...",
  73.                      "\pServers",
  74.                      FALSE,
  75.                      &(TheTarget.location),
  76.                      &thePortInfo,
  77.                      MyPPCBrowserFilter,
  78.                      "\pUNIX PPCToolbox");
  79.  
  80.     if (err == userCanceledErr) return;
  81.     
  82.     if (err) {
  83.         AlertUser(ePPCError);
  84.         return;
  85.     }
  86.     
  87. // to allow connection to the local host simply comment out this if statement
  88.     if (TheTarget.location.locationKindSelector == 0) {
  89.         AlertUser(eOurMachine);
  90.         BlockMove("Local Connection",hostname,17);
  91.         return;
  92.     }
  93.  
  94.     TheTarget.name = thePortInfo.name;
  95.     
  96.     if (TheTarget.location.locationKindSelector == 0)
  97.         BlockMove("\pLocal Connection",&hostname,18);
  98.     else
  99.         BlockMove(TheTarget.location.u.nbpEntity.objStr,&hostname,sizeof(hostname));
  100.         
  101.     (void)snd_null();
  102.     
  103.     if (snd_version() == FALSE) return;
  104.     
  105.     (void)snd_interval(TheInterval);
  106.     
  107.     (void)New_Window();
  108.     
  109. }
  110.  
  111. #pragma segment main
  112. //MW Changed prototype style to new C (old C to new C)
  113. void Javelin_AlertUser(short error)
  114. {
  115.     short        itemHit;
  116.     Str255        message;
  117.  
  118.     SetCursor(&qd.arrow);
  119.     GetIndString(message, kJavelinStrings, error);
  120.     ParamText(message, "\p", "\p", "\p");
  121.     itemHit = Alert(rUserAlert, nil);
  122. } /* Javelin_AlertUser */
  123.  
  124.  
  125. static pascal Boolean MyPPCBrowserFilter(LocationNamePtr /* theLocation */, PortInfoPtr thePortInfo)
  126. {
  127. OSType    type;
  128.     
  129.     if (thePortInfo->name.portKindSelector == ppcByString)
  130.     {
  131.         // The BlockMove is so that we don't get an address error on a
  132.         // 68000-based machine due to referencing a long at an odd-address.
  133.         BlockMove(thePortInfo->name.u.portTypeStr + 1, &type, sizeof(type));
  134.         if (type == kSignature)
  135.             return TRUE;
  136.     }
  137.     return FALSE;
  138. }
  139.  
  140. typedef struct _AEHandler {
  141.     AEEventID theAEEventID;
  142.     AEEventHandlerProcPtr handler;
  143. } AEHandler;
  144.  
  145.  
  146. AEHandler handlerTable[] = {
  147.     {kAEHeartBeat, (AEEventHandlerProcPtr)HandleHeartBeat},
  148.     {kAEJavMsg, (AEEventHandlerProcPtr)HandleMsg},
  149.     {kAEQuit, (AEEventHandlerProcPtr)HandleQuit}
  150. };
  151.  
  152.  
  153. void InstallAppleEventHandlers(void)
  154. {
  155. int i;
  156.  
  157.     for (i = 0; i < sizeof(handlerTable) / sizeof(AEHandler); i++)
  158.     {
  159.         AEInstallEventHandler(kAEAUXSuite, handlerTable[i].theAEEventID, (AEEventHandlerProcPtr)handlerTable[i].handler, 0L, false);
  160.     }
  161.  
  162. }
  163.  
  164.  
  165. pascal OSErr HandleMsg(AppleEvent *msg,AppleEvent *reply,long refcon)
  166. {
  167. DescType returnedType;
  168. Size returnedSize;
  169. int msgcode;
  170. char tmpdata[1024];
  171. int retval;
  172. WindowPtr    window;
  173. Rect r;
  174.  
  175.     retval=AEGetParamPtr (msg, keyMessageCode, typeInteger, &returnedType,&msgcode, sizeof(msgcode), &returnedSize);
  176.     retval=AEGetParamPtr (msg, keyMsgString, typeChar, &returnedType,tmpdata, 1024, &returnedSize);
  177.     tmpdata[returnedSize-1] = '\0';
  178.     
  179.     window = FrontWindow();
  180.     if ( IsAppWindow(window) ) {
  181.         r=window->portRect;
  182.         SetRect(&r,0,5,450,28);
  183.         TextBox((Ptr)tmpdata, returnedSize, &r, teJustCenter);
  184.     }
  185.  
  186.     return(noErr);
  187.  
  188. }
  189.  
  190.  
  191.  
  192. void snd_interval(int interval)
  193. {
  194. char status;
  195. char ErrorString[64];
  196. AppleEvent theAppleEvent;
  197. AppleEvent reply;
  198. AESendMode sendMode;
  199. AESendPriority sendPriority;
  200. AEAddressDesc target;
  201. DescType returnedType;
  202. Size returnedSize;
  203. long timeOutInTicks;
  204. int retval, result;
  205. long ival;
  206.  
  207.     ival=(long)interval;
  208.  
  209.     retval=AECreateDesc(typeTargetID,&TheTarget,sizeof(TargetID),&target);
  210.     retval=AECreateAppleEvent(kAEAUXSuite,kAEInterval,&target, kAutoGenerateReturnID,kAnyTransactionID,&theAppleEvent);
  211.     retval=AEPutParamPtr(&theAppleEvent,keySessionID,typeLongInteger,&TheTarget.sessionID,sizeof(TheTarget.sessionID));
  212.     retval=AEPutParamPtr(&theAppleEvent,keyInterval,typeLongInteger,&ival,sizeof(ival));
  213.     sendMode = kAEWaitReply;
  214.     sendPriority = kAENormalPriority;
  215.     timeOutInTicks = kAEDefaultTimeout;
  216.     retval=AESend(&theAppleEvent,&reply,sendMode,sendPriority,timeOutInTicks,NULL,NULL);
  217.     AEDisposeDesc(&target);
  218.     AEDisposeDesc(&theAppleEvent);
  219.     if (retval == noErr) {
  220.         result = AEGetParamPtr(&reply,keyErrorNumber,typeChar,&returnedType,(Ptr)&status,sizeof(char),&returnedSize);
  221.         result = AEGetParamPtr(&reply,keyErrorString,typeChar,&returnedType,(Ptr)ErrorString,64,&returnedSize);
  222.         ErrorString[returnedSize]='\0';
  223.         if (status == 'F') {
  224.             Javelin_AlertUser(eInterval);
  225.         }
  226.     }
  227.     else
  228.         AlertUser(eAESend);
  229. }
  230.